<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/14 0014
 * Time: 9:08
 */


if (!function_exists('get_daynum')) {
// 两日期相差多少天
	function get_daynum( $fday , $tday )
	{
		$day = ( strtotime( $tday ) - strtotime( $fday ) ) / 3600 / 24;
		return $day;
	}
}

if (!function_exists('get_minutenum')) {
// 两时间相差多少分钟
	function get_minutenum( $fday , $tday )
	{
		$day = ( strtotime( $tday ) - strtotime( $fday ) ) / 60;
		return intval( $day );
	}
}
if (!function_exists('get_username')) {
	function get_username( $id )
	{
		if ( empty( $id ) ) {
			return '未设置';
		}
		$nickname = db( 'user' ) -> where( 'uid' , $id ) -> column( 'nickname' );
		if ( is_array($nickname) ) {
			return $nickname[ 'nickname' ];
		} else {
			return '未设置';
		}
	}
}

if (!function_exists('get_userphone')) {
	function get_userphone( $id )
	{
		if ( empty( $id ) ) {
			return '未设置';
		}
		$nickname = db( 'user' ) -> where( 'uid' , $id ) -> column( 'phone' );
		if ( $nickname ) {
			return $nickname[ 'phone' ];
		} else {
			return '未设置';
		}
	}
}

if (!function_exists('get_usernamearr')) {
	function get_usernamearr( $id )
	{
		if ( empty( $id ) ) {
			return '未绑定';
		}
		$idarr = explode( ',' , $id );
		//$nickname='';
		foreach ( $idarr as $v ) {
			if ( db( 'user' ) -> where( 'uid' , $v ) -> column( 'nickname' ) ) {
				$nickname = db( 'user' ) -> where( 'uid' , $v ) -> column( 'nickname' );
			}
		}
		if ( !empty( $nickname ) ) {
			return implode( ',',$nickname );
		} else {
			return '未设置';
		}
	}
}

if (!function_exists('get_jihuolp')) {
	function get_jihuolp( $uid )
	{
		if ( empty( $uid ) ) {
			return 'ID错误';
		}
		$res = db( 'lianpiao' ) -> where( 'jihuorenid' , $uid ) -> column( 'lianpiaono' );
		if ( $res ) {
			return $res[ 'lianpiaono' ];
		} else {
			return '无';
		}
	}
}

if (!function_exists('get_bangdinglp')) {
	/**
	 * 大表全表扫描 性能问题
	 * @param $uid
	 * @return string
	 */
	function get_bangdinglp( $uid )
	{
		if ( empty( $uid ) ) {
			return 'ID错误';
		}
		$res = db( 'lianpiao' ) -> where( "find_in_set($uid,bangdingrenid)" ) -> field( 'lianpiaono' ) -> select();
		if ( $res ) {
			foreach ( $res as $v ) {
				$ret[] = $v[ 'lianpiaono' ];
			}
			return implode( ',' , $ret );
		} else {
			return '无';
		}
	}
}

if (!function_exists('get_indexads')) {
	function get_indexads()
	{
		$indexads = db( 'addon_advertising_ad' ) -> where( 'posid' , 1 ) -> find();
		return unserialize( $indexads[ 'data' ] );
	}
}

if (!function_exists('get_links')) {
	function get_links()
	{
		$indexads = db( 'addon_link' ) -> select();
		return $indexads;
	}
}

if (!function_exists('get_pihaoname')) {
	function get_pihaoname( $id )
	{
		$pname = db( 'pihao' ) -> field( 'pname' ) -> find( $id );

		if ( !empty( $pname ) ) {
			return $pname[ 'pname' ];
		} else {
			return '未设置';
		}
	}
}

if (!function_exists('getZuheName')) {
	function getZuheName( $id )
	{
		$zname = db( 'zuhe' ) -> field( 'zname' )->find($id);
		if ( !empty( $zname ) ) {
			return $zname[ 'zname' ];
		} else {
			return '未设置';
		}
	}
}

if (!function_exists('get_RightTopAds')) {
	function get_RightTopAds()
	{
		$indexads = db( 'addon_advertising_ad' ) -> where( 'posid' , 3 ) -> find();
		return unserialize( $indexads[ 'data' ] );
	}
}

if (!function_exists('getArea')) {
	function getArea( $province = null , $city = null , $town = null )
	{
		$str = '';
		$where = '';
		if ( !empty( $province ) ) {
			$where .= $province . ',';
		}
		if ( !empty( $city ) ) {
			$where .= $city . ',';
		}
		if ( !empty( $town ) ) {
			$where .= $town . ',';
		}
		$where = substr( $where , 0 , -1 );
		if ( $province == '' || $city == '' || $town == '' ) {
			return null;
		}
		$result = db( 'city' ) -> where( "cid in ($where)" ) -> field( 'lname' ) -> select();
		foreach ( $result as $d ) {
			$str .= $d[ 'lname' ] . ' ';
		}
		return $str;
	}
}

if (!function_exists('special')) {
	function special( $zuhe_id )
	{
		if ( empty( $zuhe_id ) ) {
			return false;
		}
		$result = db( 'zuhe' )
			-> where( 'id' , $zuhe_id )
			-> where( 'special' , 1 )
			-> field( 'id' ) -> find();
		if ( empty( $result ) ) {
			return false;
		} else {
			return true;
		}
	}
}

if (!function_exists('amount_format')) {
	function amount_format( $d )
	{
		$d = $d / 100;
		return $d . "元";
	}
}

if (!function_exists('idcard')) {
	function idcard( $d )
	{
		$ts = BaiduApi ::getinstance();
		if ( $ts -> idcard( $d ) ) {
			return $ts -> result[ 'address' ];
		} else {
			return $ts -> result[ 'retMsg' ];
		}

	}
}

if (!function_exists('xdeode')) {
	/**
	 * dz加密解密
	 * @param string $string 需要加密解密的字符串
	 * @param string $operation 加密|解密 DECODE解密，其他为加密
	 * @param int $expiry 有效时间 秒
	 * @return string
	 */
	function xdeode( $string , $operation = 'DECODE' , $expiry = 0 )
	{
		$ckey_length = 4;
		$key = md5( config( 'gobal.authkey' ) );
		$keya = md5( substr( $key , 0 , 16 ) );
		$keyb = md5( substr( $key , 16 , 16 ) );
		$keyc = $ckey_length ? ( $operation == 'DECODE' ? substr( $string , 0 , $ckey_length ) : substr( md5( microtime() ) , -$ckey_length ) ) : '';

		$cryptkey = $keya . md5( $keya . $keyc );
		$key_length = strlen( $cryptkey );

		$string = $operation == 'DECODE' ? base64_decode( substr( $string , $ckey_length ) ) : sprintf( '%010d' , $expiry ? $expiry + time() : 0 ) . substr( md5( $string . $keyb ) , 0 , 16 ) . $string;
		$string_length = strlen( $string );

		$result = '';
		$box = range( 0 , 255 );

		$rndkey = array();
		for ( $i = 0 ; $i <= 255 ; $i++ ) {
			$rndkey[ $i ] = ord( $cryptkey[ $i % $key_length ] );
		}

		for ( $j = $i = 0 ; $i < 256 ; $i++ ) {
			$j = ( $j + $box[ $i ] + $rndkey[ $i ] ) % 256;
			$tmp = $box[ $i ];
			$box[ $i ] = $box[ $j ];
			$box[ $j ] = $tmp;
		}

		for ( $a = $j = $i = 0 ; $i < $string_length ; $i++ ) {
			$a = ( $a + 1 ) % 256;
			$j = ( $j + $box[ $a ] ) % 256;
			$tmp = $box[ $a ];
			$box[ $a ] = $box[ $j ];
			$box[ $j ] = $tmp;
			$result .= chr( ord( $string[ $i ] ) ^ ( $box[ ( $box[ $a ] + $box[ $j ] ) % 256 ] ) );
		}

		if ( $operation == 'DECODE' ) {
			if ( ( substr( $result , 0 , 10 ) == 0 || substr( $result , 0 , 10 ) - time() > 0 ) && substr( $result , 10 , 16 ) == substr( md5( substr( $result , 26 ) . $keyb ) , 0 , 16 ) ) {
				return substr( $result , 26 );
			} else {
				return '';
			}
		} else {
			return $keyc . str_replace( '=' , '' , base64_encode( $result ) );
		}

	}
}


if (!function_exists('holiday')) {
	function holiday( $yudingdate )
	{
		$dateList = array(
			'2016-06-09' ,
			'2016-06-10' ,
			'2016-06-11' ,
			'2016-09-15' ,
			'2016-09-16' ,
			'2016-09-17' ,
			'2016-10-01' ,
			'2016-10-02' ,
			'2016-10-03' ,
			'2016-10-04' ,
			'2016-10-05' ,
			'2016-10-06' ,
			'2016-10-07' ,
		);
		if ( date( 'w' , strtotime( $yudingdate ) ) > 4 or in_array( $yudingdate , $dateList ) ) {
			return false;
		}
		return true;
	}
}

if (!function_exists('logs_write')) {
	function logs_write( $log , $client_path = null , $filename = null , $file_size = 5 )
	{
		$ds = DIRECTORY_SEPARATOR;

		if ( defined( 'ROOT_PATH' ) ) {
			$root_path = ROOT_PATH . $ds . 'Public' . $ds . 'log' . $ds;
		} else {
			$app_path = dirname( $_SERVER[ 'SCRIPT_FILENAME' ] ) . $ds;
			$root_path = dirname( $app_path ) . $ds . 'Public' . $ds . 'log' . $ds;
		}

		$config = array(
			'file_size' => $file_size * 1024 * 2014 ,
			'path'      => empty( $client_path ) ? $root_path : $root_path . $client_path . $ds
		);

		if ( empty( $filename ) ) {
			$destination = $config[ 'path' ] . date( 'Ym' ) . $ds . date( 'd' ) . '.log';
		} else {
			$destination = $filename . '.log';
		}

		$path = dirname( $destination );
		!is_dir( $path ) && mkdir( $path , 0777 , true );

		//检测日志文件大小，超过配置大小则备份日志文件重新生成
		if ( is_file( $destination ) && floor( $config[ 'file_size' ] ) <= filesize( $destination ) ) {
			rename( $destination , dirname( $destination ) . $ds . $_SERVER[ 'REQUEST_TIME' ] . '-' . basename( $destination ) );
		}
		$txt = $log . "\r\n\r\n";
		$myfile = fopen( $destination , "a" );
		if ( fwrite( $myfile , $txt ) ) {
			$flag = true;
		} else {
			$flag = false;
		}
		fclose( $myfile );
		return $flag;
	}
}

if (!function_exists('isCreditNo')) {
	function isCreditNo( $vStr )
	{
		$vCity = array(
			'11' , '12' , '13' , '14' , '15' , '21' , '22' ,
			'23' , '31' , '32' , '33' , '34' , '35' , '36' ,
			'37' , '41' , '42' , '43' , '44' , '45' , '46' ,
			'50' , '51' , '52' , '53' , '54' , '61' , '62' ,
			'63' , '64' , '65' , '71' , '81' , '82' , '91'
		);

		if ( !preg_match( '/^([\d]{17}[xX\d]|[\d]{15})$/' , $vStr ) )
			return false;

		if ( !in_array( substr( $vStr , 0 , 2 ) , $vCity ) )
			return false;

		$vStr = preg_replace( '/[xX]$/i' , 'a' , $vStr );
		$vLength = strlen( $vStr );

		if ( $vLength == 18 ) {
			$vBirthday = substr( $vStr , 6 , 4 ) . '-' . substr( $vStr , 10 , 2 ) . '-' . substr( $vStr , 12 , 2 );
		} else {
			$vBirthday = '19' . substr( $vStr , 6 , 2 ) . '-' . substr( $vStr , 8 , 2 ) . '-' . substr( $vStr , 10 , 2 );
		}

		if ( date( 'Y-m-d' , strtotime( $vBirthday ) ) != $vBirthday )
			return false;
		if ( $vLength == 18 ) {
			$vSum = 0;

			for ( $i = 17 ; $i >= 0 ; $i-- ) {
				$vSubStr = substr( $vStr , 17 - $i , 1 );
				$vSum += ( pow( 2 , $i ) % 11 ) * ( ( $vSubStr == 'a' ) ? 10 : intval( $vSubStr , 11 ) );
			}

			if ( $vSum % 11 != 1 )
				return false;
		}

		return true;
	}
}

if (!function_exists('random')) {
	/**
	 * 随机字符生成
	 * @param int $lenght 长度
	 * @param string $type 随机字符范围 all：特殊字符+字母大小写+数字 letter：字母大小写 upper：字母大写 lower：字母小写 int：数字
	 * @return string 随机字符串
	 */
	function random( $lenght = 6 , $type = "" )
	{
		$str_pol = "";
		$n = 0;
		switch ( $type ) {
			case "all" :
				$str_pol .= "~!@#$%&*()_+|-=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
				break;
			case "letter" :
				$str_pol .= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
				break;
			case "upper" :
				$str_pol .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
				break;
			case "lower" :
				$str_pol .= "abcdefghijklmnopqrstuvwxyz";
				break;
			case "int" :
				$str_pol .= "0123456789";
				break;
			default :
				$str_pol .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
		}
		$len = strlen( $str_pol );
		if ( $len < $lenght ) {
			$n = ceil( $lenght / $len );
			for ( $i = 1 ; $i < $n ; $i++ ) {
				$str_pol .= $str_pol;
			}
		}
		return substr( str_shuffle( $str_pol ) , 0 , $lenght );
	}
}


if (!function_exists('username')) {
	function username()
	{
		return "lianpiao_" . time() . rand( 10 , 99 );
	}
}

if(!function_exists('genRandomString')){
	function genRandomString($len) {
		$chars = array (
				"0",
				"1",
				"2",
				"3",
				"4",
				"5",
				"6",
				"7",
				"8",
				"9" 
		);
		$charsLen = count ( $chars ) - 1;
		
		shuffle ( $chars ); // 将数组打乱
		
		$output = "";
		for($i = 0; $i < $len; $i ++) {
			$output .= $chars [mt_rand ( 0, $charsLen )];
		}
		
		return $output;
	}
}
